package com.hzcx.app.simplechat.ui.moment.adapter;

import android.text.Html;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.moment.bean.NewMessageListBean;
import com.hzcx.app.simplechat.util.glide.GlideUtils;

import java.util.List;

public class NewMessageAdapter extends BaseQuickAdapter<NewMessageListBean, BaseViewHolder> {

    private static final int TYPE_FABULOUS = 1;
    private static final int TYPE_COMMENT = 2;
    private static final int TYPE_REPLY = 3;
    private static final int TYPE_DEFAULT = 0;

    private static final int FABULOUS = R.layout.rv_item_new_msg_fabulous;
    private static final int COMMENT = R.layout.rv_item_new_msg_comment;
    private static final int REPLY = R.layout.rv_item_new_msg_reply;
    private static final int DEFAULT = R.layout.rv_item_chat_message_default;

    public NewMessageAdapter(@Nullable List<NewMessageListBean> data) {
        super(data);
        setMultiTypeDelegate(new MultiTypeDelegate<NewMessageListBean>() {
            @Override
            protected int getItemType(NewMessageListBean newMessageListBean) {
                if (newMessageListBean.getType() == TYPE_FABULOUS) {
                    return TYPE_FABULOUS;
                } else if (newMessageListBean.getType() == TYPE_COMMENT) {
                    return TYPE_COMMENT;
                } else if (newMessageListBean.getType() == TYPE_REPLY) {
                    return TYPE_REPLY;
                }
                return DEFAULT;
            }
        });

        getMultiTypeDelegate().registerItemType(TYPE_FABULOUS, FABULOUS)
                .registerItemType(TYPE_COMMENT, COMMENT)
                .registerItemType(TYPE_REPLY, REPLY)
                .registerItemType(TYPE_DEFAULT, DEFAULT);
    }

    @Override
    protected void convert(BaseViewHolder helper, NewMessageListBean item) {

        GlideUtils.loadImg(mContext, item.getMember_avatar(), helper.getView(R.id.iv_head));
        helper.setText(R.id.tv_nick_name, item.getMember_nickname());
        if (item.getDynamic_images() != null && item.getDynamic_images().size() > 0) {
            helper.setVisible(R.id.iv_moment_img, true);
            helper.setVisible(R.id.tv_moment_content, false);
            try {
                GlideUtils.loadImgAsCenter(mContext, item.getDynamic_images().get(0).getImgurl(), helper.getView(R.id.iv_moment_img));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            helper.setVisible(R.id.iv_moment_img, false);
            helper.setVisible(R.id.tv_moment_content, true);
            helper.setText(R.id.tv_moment_content, item.getDynamic_content());
        }
        helper.setText(R.id.tv_date, item.getCreatetime());

        if (item.getType() == TYPE_COMMENT) {
            helper.setText(R.id.tv_content, item.getReview_content());
        }

        if (item.getType() == TYPE_REPLY) {
            TextView tvContent = helper.getView(R.id.tv_content);
            String str="回复<strong>" + item.getBymember_nickname() + "：</strong>" + item.getReview_content();
            tvContent.setText(Html.fromHtml(str));
        }

        helper.addOnClickListener(R.id.iv_head);
    }
}
